<script setup>
import { RouterView } from 'vue-router'
import { ref } from 'vue'
import { Menu } from '@element-plus/icons-vue' 

// 使用 JSON 维护菜单
const menuItems = ref([
  {
    index: '/',
    label: 'Home',
    children: []
  },
  {
    index: '/about',
    label: 'About',
    children: []
  },
  {
    index: '/game',
    label: 'game',
    children: [
      {
        index: '/game/game01',
        label: 'game01'
      },
      {
        index: '/game/game02',
        label: 'game02'
      }
    ]
  }
])
// 控制菜单是否收缩
const isCollapse = ref(false)
// 右侧内容固定宽度
const rightContentWidth = 'calc(100vw - 200px)'

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
  // 根据菜单状态调整右侧内容宽度
  rightContentWidth.value = isCollapse.value ? 'calc(100vw - 64px)' : 'calc(100vw - 200px)'
}
</script>

<template>
  <div style="display: flex; height: 100vh;">
    <!-- 左侧菜单 -->
    <el-aside :width="isCollapse ? '64px' : '200px'" class="menu-aside">
      <el-menu
        :collapse="isCollapse"
        :collapse-transition="true"
        default-active="/"
        class="el-menu-vertical-demo"
        router
      >
        <template v-for="item in menuItems" :key="item.index">
          <el-sub-menu v-if="item.children.length" :index="item.index">
            <template #title>
              <el-icon><Menu /></el-icon>
              <span>{{ item.label }}</span>
            </template>
            <template #default>
              <el-menu-item v-for="child in item.children" :key="child.index" :index="child.index">
                {{ child.label }}
              </el-menu-item>
            </template>
          </el-sub-menu>
          <el-menu-item v-else :index="item.index">
            <el-icon><Menu /></el-icon>
            <span>{{ item.label }}</span>
          </el-menu-item>
        </template>
      </el-menu>
      <el-button
        @click="toggleCollapse"
        class="toggle-button">
        {{ isCollapse ? '>>>' : '<<<' }}
      </el-button>
    </el-aside>
    <!-- 右侧内容 -->
    <div :style="{ width: rightContentWidth }">
      <RouterView />
    </div>
  </div>
</template>

<style scoped>
.menu-aside {
  transition: width 0.3s ease; /* 添加宽度过渡动画 */
  will-change: width; /* 告诉浏览器宽度即将改变，进行优化 */
  transform: translateZ(0); /* 开启硬件加速 */
}

nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
}

nav a.router-link-exact-active {
  color: var(--color-text);
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
  border: 0;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;
    padding: 1rem 0;
    margin-top: 1rem;
  }
}
.toggle-button {
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 阴影 */
  transition: all 0.3s ease; /* 过渡效果 */
  margin: 10px 0; /* 仅保留上下外边距 */
  width: 100%; /* 宽度填满菜单 */
  border-radius: 4px; /* 圆角 */
}

.toggle-button:hover {
  transform: translateY(-1px); /* 悬停时上移 */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 悬停时加深阴影 */
}
</style>
